package org.icress.fusion.b2b

import grails.converters.JSON
//import org.icress.fusion.wf.WorkflowHistory

class SalesOrderController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def salesOrderService
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        params.status = params.status ?: ''
        def salesOrderInstanceList = salesOrderService.list(params)
        render view : '/b2b/salesOrder/list', model : [salesOrderInstanceList: salesOrderInstanceList, salesOrderInstanceTotal: SalesOrder.countByStatus(params.status)]
    }

    def create = {
        def salesOrderInstance = new SalesOrder()
        salesOrderInstance.properties = params
        def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
        if(pageLayoutName) {
            salesOrderInstance = salesOrderService.initSalesOrder(salesOrderInstance)
            render view : '/b2b/salesOrder/create', model : [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName]
        }else {
            render(view : "/unauthorized")
        }
    }


    def save = {
        def salesOrderInstance = new SalesOrder(params)
        
        if (salesOrderService.save(salesOrderInstance)) {
            flash.message = "${message(code: 'b2b.salesOrder.created', args: [ salesOrderInstance.id])}"
            redirect(action: "show", id: salesOrderInstance.id)
        }else{
            def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
            render(view: "/b2b/salesOrder/create", model: [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName])
        }
    }

    def show = {
        def salesOrderInstance = salesOrderService.show(params.id)
        if (!salesOrderInstance) {
            flash.message = "${message(code: 'b2b.salesOrder.not.found', args: [params.id])}"
            redirect(action: "list")
        }else {
            def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
            //def workflowHistoryInstanceList = salesOrderService.workflowHistory(salesOrderInstance.id)
            if(pageLayoutName) {
                render view : "/b2b/salesOrder/show", model : [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName]
            }else {
                render(view : "/unauthorized")
            }
        }
    }

    def edit = {
		if(salesOrderService.isLocked(params.id) == true){
			render(view : "/unauthorized") 
            return
		}
        def salesOrderInstance = salesOrderService.edit(params.id)
        if (!salesOrderInstance) {
            flash.message = "${message(code: 'b2b.salesOrder.not.found', args: [ params.id])}"
            redirect(action: "list")
        }else {
            def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
            if(pageLayoutName) {
                render view : '/b2b/salesOrder/edit', model : [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName]
            }else {
                render(view : "/unauthorized")
            }
        }
    }
    
    def update = {
		if(salesOrderService.isLocked(params.id) == true){
			render(view : "/unauthorized") 
            return
		}
	
        def salesOrderInstance = salesOrderService.edit(params.id)
        if (salesOrderInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (salesOrderInstance.version > version) {
                    
                    salesOrderInstance.errors.rejectValue("version", "b2b.salesOrder.optimistic.locking.failure", "Another user has updated this SalesOrder while you were editing")
                    def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
                    render view : '/b2b/salesOrder/edit', model : [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName]
                    return
                }
            }
            
            if (salesOrderService.update(salesOrderInstance, params)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'b2b.salesOrder.label', default: 'SalesOrder'), salesOrderInstance.id])}"
                redirect(action: "show", id: salesOrderInstance.id)
            }else {
                def pageLayoutName = salesOrderService.pageLayoutName(salesOrderInstance.recordType.id)
                render view : '/b2b/salesOrder/edit', model : [salesOrderInstance: salesOrderInstance, pageLayoutName : pageLayoutName]
                //render controller : 'index', action : 'index'
            }
        } else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.salesOrder.label', default: 'SalesOrder'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
		if(salesOrderService.isLocked(params.id) == true){
			render(view : "/unauthorized") 
            return
		}
	
	
        def salesOrderInstance = salesOrderService.delete(params.id)
        if (salesOrderInstance) {
            try {
                salesOrderInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'b2b.salesOrder.label', default: 'SalesOrder'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'b2b.salesOrder.label', default: 'SalesOrder'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.salesOrder.label', default: 'SalesOrder'), params.id])}"
            redirect(action: "list")
        }
    }
    
    def ajaxShow = {
        render SalesOrder.get(1) as JSON
    }
}
